package io.bdmc.modules.bss.controller;

import com.alibaba.fastjson2.JSONObject;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceDevice;
import io.bdmc.modules.bss.service.IDeviceDeviceService;
import io.bdmc.modules.bss.service.IDeviceMachineDtsConfigService;
import io.bdmc.modules.sys.controller.AbstractController;
import io.bdmc.core.dts.DtsDeviceUtil;
import io.bdmc.core.msp2000.Msp2000AlarmProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ping
 * @since 2021-11-19
 */
@RestController
@RequestMapping("/bss/dts")
public class DeviceMachineDtsController extends AbstractController {

    @Autowired
    IDeviceDeviceService deviceService;

    @Autowired
    DtsDeviceUtil dtsDeviceUtil;

    @Autowired
    IDeviceMachineDtsConfigService dtsConfigService;

    @Autowired
    Msp2000AlarmProcess _msp2000AlarmProcess;

    @GetMapping("/config")
    public R readConfig(Integer deviceId) {
        final DeviceDevice dts = deviceService.getById(deviceId);
        boolean result = dtsDeviceUtil.readDtsConfigFromDevice(dts.getIpaddr(), dts.getDevicePort());
        return R.ok().putData(result);
    }

    @GetMapping("/global")
    // "查询dts配置"
    public R getGlobal(Integer deviceId) {
        final DeviceDevice dts = deviceService.getById(deviceId);
        final Map<String, Object> globalConfig = dtsDeviceUtil.getGlobalConfig(dts.getIpaddr());
        return R.ok().putData(globalConfig);
    }

    @GetMapping("/read")
    public R read(Integer deviceId) {
        final DeviceDevice dts = deviceService.getById(deviceId);
        final Map<String, Object> globalConfig = dtsDeviceUtil.getGlobalConfig(dts.getIpaddr());
        return R.ok().putData(globalConfig);
    }

    @PostMapping("/global/{deviceId}")
    // 给dts下发配置
    public R setGlobal(@PathVariable String deviceId, @RequestBody JSONObject json) {
        final DeviceDevice dts = deviceService.getById(deviceId);
        final String config = dtsDeviceUtil.setGlobalConfig(json, dts.getIpaddr());
        boolean sendResult = dtsDeviceUtil.sendToDtsDevice(dts.getIpaddr(), dts.getDevicePort(), config);
        return R.trueOrFalse(sendResult);
    }

    @GetMapping("/channel")
    // "查询dts配置"
    public R getChannelConfig(Integer deviceId, Integer channel) {

        final DeviceDevice dts = deviceService.getById(deviceId);
        final Map<String, Object> channelConfig = dtsDeviceUtil.getChannelConfig(dts.getIpaddr(), channel);
        return R.ok().putData(channelConfig);
    }

    @PostMapping("/channel/{deviceId}/{channel}")
    // 给dts下发配置
    public R setChannelConfig(@PathVariable Integer deviceId, @PathVariable Integer channel,
            @RequestBody JSONObject json) {
        final DeviceDevice dts = deviceService.getById(deviceId);
        logger.info(json.toJSONString());
        final String config = dtsDeviceUtil.setChannelConfig(json, dts.getIpaddr(), channel);
        boolean sendResult = dtsDeviceUtil.sendToDtsDevice(dts.getIpaddr(), dts.getDevicePort(), config);
        if (sendResult) {
            dtsDeviceUtil.readDtsConfigFromDevice(dts.getIpaddr(), dts.getDevicePort());
        }
        return R.trueOrFalse(sendResult);
    }

    @GetMapping("/curve/origin")
    public R getOrigin(Integer deviceId, Integer channel) {

        final DeviceDevice dts = deviceService.getById(deviceId);
        final Map<String, Object> channelConfig = dtsDeviceUtil.getOriginCurve(dts.getIpaddr(), dts.getDevicePort(),
                channel);
        return R.ok().putData(channelConfig);
    }

    @GetMapping("/curve/divided")
    public R getOriginDivided(Integer deviceId, Integer channel) {

        final DeviceDevice dts = deviceService.getById(deviceId);
        final List<Double> channelConfig = dtsDeviceUtil.getOriginDividedCurve(dts.getIpaddr(), dts.getDevicePort(),
                channel);
        return R.ok().putData(channelConfig);
    }

    @GetMapping("/curve/temp")
    public R getTemp(Integer deviceId, Integer channel) {

        final DeviceDevice dts = deviceService.getById(deviceId);
        final List<Double> tempCurve = dtsDeviceUtil.getTempCurve(dts.getIpaddr(), dts.getDevicePort(), channel);
        if (tempCurve != null && tempCurve.size() > 0) {
            _msp2000AlarmProcess.dtsAlarm(deviceId, channel, tempCurve);
        }
        return R.ok().putData(tempCurve);
    }

    @GetMapping("/temp")
    public R temp(Integer deviceId) {

        final DeviceDevice dts = deviceService.getById(deviceId);
        final double tempCurve = dtsDeviceUtil.getDtsTemp(dts.getIpaddr(), dts.getDevicePort());
        return R.ok().putData(tempCurve);
    }

    // 读取fixedSwitch
    @GetMapping("/fixed")
    public R fixed(Integer deviceId, Integer channel) {

        final DeviceDevice dts = deviceService.getById(deviceId);
        final int tempCurve = dtsDeviceUtil.getTemperatureCurveFixedSwitch(dts.getIpaddr(), dts.getDevicePort(),
                channel);
        return R.ok().putData(tempCurve);
    }

    @PostMapping("/fixed/{deviceId}/{channel}")
    // 给dts下发配置fixedSwitch
    public R fixed(@PathVariable Integer deviceId, @PathVariable Integer channel, @RequestBody JSONObject json) {
        int fixedSwitch = json.getInteger("fixedSwitch");
        final DeviceDevice dts = deviceService.getById(deviceId);
        final boolean setResult = dtsDeviceUtil.setTemperatureCurveFixedSwitch(dts.getIpaddr(), dts.getDevicePort(),
                channel, fixedSwitch);
        return R.ok().putData(setResult);
    }

    @PostMapping("/correct/{deviceId}/{channel}")
    // 给dts下发配置fixedSwitch
    public R correct(@PathVariable Integer deviceId, @PathVariable Integer channel, @RequestBody JSONObject json) {
        int fixedSwitch = json.getInteger("fixedSwitch");
        final DeviceDevice dts = deviceService.getById(deviceId);
        final boolean setResult = dtsDeviceUtil.setTemperatureCurveAutocorrection(dts.getIpaddr(), dts.getDevicePort(),
                channel, fixedSwitch);
        return R.ok().putData(setResult);
    }
}
